/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.customControllers;

import com.bookshop.entity.Customer;
import com.bookshop.facade.CustomerFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Samir
 */
@ManagedBean(name = "myAuthentificationController")
@SessionScoped
public class MyAuthentificationBean implements Serializable {

    @EJB
    private CustomerFacade customerFacade;
    private Customer customer = new Customer();
    private String login;
    private String password;
    private String message = "";
    private boolean connecte = false;

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public boolean isConnecte() {
        return connecte;
    }

    public void setConnecte(boolean connecte) {
        this.connecte = connecte;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Creates a new instance of MyAuthentificationBean
     */
    public MyAuthentificationBean() {
    }

    public String connect() {
        try {
            this.customer = customerFacade.getCustomerAuthentifie(login, password);
            if (getLogin().equals(customer.getLogin()) && getPassword().equals(customer.getPassword())) {
                
                message = "Bonjour : " + customer.getLastName();
                setConnecte(true);
            return "connecte_ok";    
            } else {
                setConnecte(false);
                message = "Erreur d'authentification";
                return "connecte_ko";
            }
        } catch (Exception ex) {
            System.out.println(" Authentification not correct");
        }
        return message;

    }

    public void deconnect() {
        setConnecte(false);
    }
}
